import sys

from PyQt4 import QtGui, QtCore
import pygst
import gst

import GSTCamRecordingUI

# pyuic4 gst-cam-recording.ui -o gst-cam-recording-ui.py (pyqt4-dev-tool)


class GSTCamRecording(QtGui.QMainWindow, GSTCamRecordingUI.Ui_MainWindow):
    """ The second parent must be 'Ui_<obj. name of main widget class>'.
        If confusing, simply open up ImageViewer.py and get the class
        name used. I'd named mine as mainWindow, hence Ui_mainWindow. """
 
    def __init__(self, parent=None):
        super(GSTCamRecording, self).__init__(parent)
        # This is because Python does not automatically
        # call the parent's constructor.
        self.setupUi(self)
        # Pass this "self" for building widgets and
        # keeping a reference.
        # Pass this "self" for building widgets and
        # keeping a reference.
        self.connectActions()
        # trying with gst...
        self.player = gst.Pipeline("player")
        self.source = gst.element_factory_make("v4l2src", "vsource")
        self.sink = gst.element_factory_make("autovideosink", "outsink")
        self.source.set_property("device", "/dev/video0")
        self.scaler = gst.element_factory_make("videoscale", "vscale")

        self.player.add(self.source, self.scaler, self.sink)
        gst.element_link_many(self.source,self.scaler, self.sink)
    def startPreview(self):
        self.player.set_state(gst.STATE_PLAYING)



    def connectActions(self):
        self.actionQuit.triggered.connect(QtGui.qApp.quit)
        # Connect the Quit action's triggered signal
        # to a proper Quit method
        # given by qApp (which points to your QApplication
        # object).
 
    def main(self):
        self.show()
 
if __name__=='__main__':
    app = QtGui.QApplication(sys.argv)
    gstCamRecording = GSTCamRecording()
    gstCamRecording.main()
    gstCamRecording.startPreview()
    app.exec_()


# integrate the UI file and add the ctrl-q shortcut
# http://www.harshj.com/2010/04/18/pyqt-creating-interfaces-visually-with-designer/
